package com.xthh.sql;


import android.app.Application;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.xthh._enum.HH_EnumReleaseRecordINI;
import com.xthh._function.HH_CharUtil;
import com.xthh._function.HH_TimeUtil;
import com.xthh._struct.HH_T_ReleaseRecord;

import java.util.ArrayList;
import java.util.List;


/**
 * 	api查询方式
 * */
public class HH_ReleaseRecordDao2 {

    private static final String TAG = "ReleaseRecordDao2";
    HH_XTSQLiteOpenHelper mOpenHelper;//数据库的帮助类

    public HH_ReleaseRecordDao2(Context context, Application application){
        mOpenHelper = new HH_XTSQLiteOpenHelper(context, application);
    }


    /**
     * 添加到ReleaseRecord表一条数据
     * */
    public void insert(HH_T_ReleaseRecord tr){

        HH_T_ReleaseRecord trFind = queryItem(tr.getTaskID());
        if (trFind == null){
            SQLiteDatabase db = mOpenHelper.getWritableDatabase();
            if(db.isOpen()){  //如果数据库打开

                ContentValues values = new ContentValues();
                values.put(HH_EnumReleaseRecordINI.taskID.toString(), tr.getTaskID());
                values.put(HH_EnumReleaseRecordINI.tasktype.toString(), tr.getTaskType());
                values.put(HH_EnumReleaseRecordINI.picpath.toString(), tr.getPicPath_A());
                values.put(HH_EnumReleaseRecordINI.picpath_B.toString(), tr.getPicPath_B());
                values.put(HH_EnumReleaseRecordINI.tagname.toString(), tr.getTagName());
                values.put(HH_EnumReleaseRecordINI.res.toString(), tr.getRes());
                values.put(HH_EnumReleaseRecordINI.color.toString(), tr.getColor());
                values.put(HH_EnumReleaseRecordINI.taskstate.toString(), tr.getTaskState());
                values.put(HH_EnumReleaseRecordINI.time.toString(), tr.getTime());
                values.put(HH_EnumReleaseRecordINI.logpath.toString(), tr.getLogPath());
                values.put(HH_EnumReleaseRecordINI.lastlog.toString(), tr.getLastLog());
                values.put(HH_EnumReleaseRecordINI.mac.toString(), tr.getMac());
                values.put(HH_EnumReleaseRecordINI.msg_title.toString(), tr.getMsg_title());
                values.put(HH_EnumReleaseRecordINI.msg_content.toString(), tr.getMsg_content());
                values.put(HH_EnumReleaseRecordINI.led_control_type.toString(), tr.getLed_control_type());
                values.put(HH_EnumReleaseRecordINI.led_light_type.toString(), tr.getLed_light_type());
                values.put(HH_EnumReleaseRecordINI.led_duration_second.toString(), tr.getLed_duration_second());
                values.put(HH_EnumReleaseRecordINI.otaPath.toString(), tr.getOtaPath());
                values.put(HH_EnumReleaseRecordINI.msgTaskID.toString(), tr.getMsgTaskID());
                values.put(HH_EnumReleaseRecordINI.msgTaskFontType.toString(), tr.getMsgTaskFontType());
                values.put(HH_EnumReleaseRecordINI.msgTaskMsgType.toString(), tr.getMsgTaskMsgType());

                long id = db.insert(HH_SheetName.SQL_SHEET_NAME_RELEASE_RECORD, null , values);

                Log.i(TAG, id+"");

                db.close(); //数据库关闭
            }
        }
    }

    public void deleteAll(){
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();//获得可写入数据库对象
        if(db.isOpen()){

            db.execSQL("delete from " + HH_SheetName.SQL_SHEET_NAME_RELEASE_RECORD);

            db.close();
        }
    }

     public void delete(String taskID){
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();//获得可写入数据库对象
        if(db.isOpen()){

            String whereClasuse = HH_EnumReleaseRecordINI.taskID.toString() + " = ?";
            String [] whereArgs = {taskID};
            int count = db.delete(HH_SheetName.SQL_SHEET_NAME_RELEASE_RECORD, whereClasuse, whereArgs);
            //返回值代表删除行数

            Log.i(TAG, "删除了:"+count);
            db.close();
        }
    }

    public void update_lastlog(String taskID, String lastLog){
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();

        if(db.isOpen()){
            ContentValues values = new ContentValues();
            values.put(HH_EnumReleaseRecordINI.lastlog.toString(), lastLog);
            String whereClause = HH_EnumReleaseRecordINI.taskID.toString() + " = ?" ;
            int count = db.update(HH_SheetName.SQL_SHEET_NAME_RELEASE_RECORD, values, whereClause, new String[]{taskID});
            db.close();
        }
    }
    public void update_time(String taskID, String time){
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();

        if(db.isOpen()){
            ContentValues values = new ContentValues();
            values.put(HH_EnumReleaseRecordINI.time.toString(), time);
            String whereClause = HH_EnumReleaseRecordINI.taskID.toString() + " = ?" ;
            int count = db.update(HH_SheetName.SQL_SHEET_NAME_RELEASE_RECORD, values, whereClause, new String[]{taskID});
            db.close();
        }
    }

    public void update_taskstate(String taskID, String taskState){
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();

        if(db.isOpen()){
            ContentValues values = new ContentValues();
            values.put(HH_EnumReleaseRecordINI.taskstate.toString(), taskState);
            String whereClause = HH_EnumReleaseRecordINI.taskID.toString() + " = ?" ;
            int count = db.update(HH_SheetName.SQL_SHEET_NAME_RELEASE_RECORD, values, whereClause, new String[]{taskID});
            db.close();
        }
    }

    public List<HH_T_ReleaseRecord> queryAll(){
        List<HH_T_ReleaseRecord> personList = new ArrayList<HH_T_ReleaseRecord>();

        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
        if(db.isOpen()){

            String [] columns = new String[HH_EnumReleaseRecordINI.values().length];
            for (HH_EnumReleaseRecordINI e : HH_EnumReleaseRecordINI.values()) {
                columns[e.ordinal()] = e.name();
            }

            String sCurrentTime = HH_TimeUtil.GetCurrentSystemTimeF();

            String selection = null; // 选择条件，给null查询所有
            String[] selectionArgs = null;//选择条件参数,会把选择条件中的？替换成这个数组中的值
            String groupBy = null; // 分组语句 group by name  注意些的时候需要要group by 去掉
            String having = null;  // 过滤语句
            String orderBy = null ; //排序

            Cursor cursor = db.query(HH_SheetName.SQL_SHEET_NAME_RELEASE_RECORD, columns, selection, selectionArgs, groupBy, having, orderBy);

            if(cursor != null && cursor.getCount() > 0){

                while(cursor.moveToNext()){ //向下移动一位，直到最后一位,不可以往下移动了，停止

                    HH_T_ReleaseRecord tr = new HH_T_ReleaseRecord();
                    tr.setTaskType(cursor.getString(HH_EnumReleaseRecordINI.tasktype.ordinal()));
                    tr.setPicPath_A(cursor.getString(HH_EnumReleaseRecordINI.picpath.ordinal()));
                    tr.setPicPath_B(cursor.getString(HH_EnumReleaseRecordINI.picpath_B.ordinal()));
                    tr.setTagName(cursor.getString(HH_EnumReleaseRecordINI.tagname.ordinal()));
                    tr.setRes(cursor.getString(HH_EnumReleaseRecordINI.res.ordinal()));
                    tr.setColor(cursor.getString(HH_EnumReleaseRecordINI.color.ordinal()));
                    tr.setTaskState(cursor.getString(HH_EnumReleaseRecordINI.taskstate.ordinal()));
                    tr.setTime(cursor.getString(HH_EnumReleaseRecordINI.time.ordinal()));
                    tr.setLogPath(cursor.getString(HH_EnumReleaseRecordINI.logpath.ordinal()));
                    tr.setLastLog(cursor.getString(HH_EnumReleaseRecordINI.lastlog.ordinal()));
                    tr.setMsg_title(cursor.getString(HH_EnumReleaseRecordINI.msg_title.ordinal()));
                    tr.setMsg_content(cursor.getString(HH_EnumReleaseRecordINI.msg_content.ordinal()));
                    tr.setLed_control_type(cursor.getString(HH_EnumReleaseRecordINI.led_control_type.ordinal()));
                    tr.setLed_light_type(cursor.getString(HH_EnumReleaseRecordINI.led_light_type.ordinal()));
                    tr.setLed_duration_second(cursor.getString(HH_EnumReleaseRecordINI.led_duration_second.ordinal()));
                    tr.setOtaPath(cursor.getString(HH_EnumReleaseRecordINI.otaPath.ordinal()));
                    tr.setMsgTaskID(cursor.getString(HH_EnumReleaseRecordINI.msgTaskID.ordinal()));
                    tr.setMsgTaskFontType(cursor.getString(HH_EnumReleaseRecordINI.msgTaskFontType.ordinal()));
                    tr.setMsgTaskMsgType(cursor.getString(HH_EnumReleaseRecordINI.msgTaskMsgType.ordinal()));
                    tr.setMac(cursor.getString(HH_EnumReleaseRecordINI.mac.ordinal()));
                    tr.setTaskID(cursor.getString(HH_EnumReleaseRecordINI.taskID.ordinal()));


                    if (tr.getTaskState() != null) {
                        if (!HH_CharUtil.Strcmp(tr.getTaskState() , "1", true)) {
                            // 除了成功的，其它的都为设置失败
                            tr.setTaskState("3");
                            tr.setLastLog("");
                        }
                    }

                    if (tr.getTime() != null && tr.getTime().length() == 14) {
                        // 1年后就不要了
                        long lTime = HH_TimeUtil.DateDiffHour(tr.getTime(), sCurrentTime, "yyyyMMddHHmmss");
                        if (lTime < 1 * 24 * 30 * 12) {
                            personList.add(tr);
                        }
                    }
                }
                db.close();
                return personList;
            }

            db.close();
        }

        return personList;
    }


    public HH_T_ReleaseRecord queryItem(String taskID){
        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
        if(db.isOpen()){

            String [] columns = new String[HH_EnumReleaseRecordINI.values().length];
            for (HH_EnumReleaseRecordINI e : HH_EnumReleaseRecordINI.values()) {
                columns[e.ordinal()] = e.name();
            }

            String selection = HH_EnumReleaseRecordINI.taskID.toString() + " = ?"; // 选择条件，给null查询所有
            String[] selectionArgs = {taskID};//选择条件参数,会把选择条件中的？替换成这个数组中的值
            String groupBy = null; // 分组语句 group by name  注意些的时候需要要group by 去掉
            String having = null;  // 过滤语句
            String orderBy = null ; //排序

            Cursor cursor = db.query(HH_SheetName.SQL_SHEET_NAME_RELEASE_RECORD, columns, selection, selectionArgs, groupBy, having, orderBy);

            if(cursor != null && cursor.moveToFirst()){  //cursor不位空,可以移动到第一行

                HH_T_ReleaseRecord tr = new HH_T_ReleaseRecord();
                tr.setTaskType(cursor.getString(HH_EnumReleaseRecordINI.tasktype.ordinal()));
                tr.setPicPath_A(cursor.getString(HH_EnumReleaseRecordINI.picpath.ordinal()));
                tr.setPicPath_B(cursor.getString(HH_EnumReleaseRecordINI.picpath_B.ordinal()));
                tr.setTagName(cursor.getString(HH_EnumReleaseRecordINI.tagname.ordinal()));
                tr.setRes(cursor.getString(HH_EnumReleaseRecordINI.res.ordinal()));
                tr.setColor(cursor.getString(HH_EnumReleaseRecordINI.color.ordinal()));
                tr.setTaskState(cursor.getString(HH_EnumReleaseRecordINI.taskstate.ordinal()));
                tr.setTime(cursor.getString(HH_EnumReleaseRecordINI.time.ordinal()));
                tr.setLogPath(cursor.getString(HH_EnumReleaseRecordINI.logpath.ordinal()));
                tr.setLastLog(cursor.getString(HH_EnumReleaseRecordINI.lastlog.ordinal()));
                tr.setMsg_title(cursor.getString(HH_EnumReleaseRecordINI.msg_title.ordinal()));
                tr.setMsg_content(cursor.getString(HH_EnumReleaseRecordINI.msg_content.ordinal()));
                tr.setLed_control_type(cursor.getString(HH_EnumReleaseRecordINI.led_control_type.ordinal()));
                tr.setLed_light_type(cursor.getString(HH_EnumReleaseRecordINI.led_light_type.ordinal()));
                tr.setLed_duration_second(cursor.getString(HH_EnumReleaseRecordINI.led_duration_second.ordinal()));
                tr.setOtaPath(cursor.getString(HH_EnumReleaseRecordINI.otaPath.ordinal()));
                tr.setMsgTaskID(cursor.getString(HH_EnumReleaseRecordINI.msgTaskID.ordinal()));
                tr.setMsgTaskFontType(cursor.getString(HH_EnumReleaseRecordINI.msgTaskFontType.ordinal()));
                tr.setMsgTaskMsgType(cursor.getString(HH_EnumReleaseRecordINI.msgTaskMsgType.ordinal()));
                tr.setMac(cursor.getString(HH_EnumReleaseRecordINI.mac.ordinal()));
                tr.setTaskID(cursor.getString(HH_EnumReleaseRecordINI.taskID.ordinal()));


                db.close();
                return tr;
            }

            db.close();
        }

        return null;
    }

}